package com.hu.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hu.auth.common.aop.Verify;
import com.hu.auth.common.result.R;
import com.hu.auth.model.User;
import com.hu.auth.model.po.AssignRolePo;
import com.hu.auth.model.po.UserAddPo;
import com.hu.auth.model.po.UserPo;
import com.hu.auth.service.RoleService;
import com.hu.auth.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    @Verify(key = "bnt.sysUser.list")
    @GetMapping("/list/{page}/{limit}")
    public R list(@PathVariable int page, @PathVariable int limit, UserPo po) {
        return R.ok(userService.list(page, limit, po));
    }

    @Verify(key = "bnt.sysUser.list")
    @GetMapping("/getUserById/{id}")
    public R getUserById(@PathVariable Long id) {
        if (id == null) {
            return R.fail().message("参数错误");
        }
        return R.ok(userService.getById(id));
    }

    @Verify(key = "bnt.sysUser.add")
    @PostMapping("/add")
    public R add(@RequestBody @Validated UserAddPo po) {
        if (po.getId() != null) {
            return R.fail().message("参数错误");
        }
        if (userService.count(new QueryWrapper<User>().lambda().eq(User::getName, po.getUsername())) > 0) {
            return R.fail().message("角色名出现重复");
        }
        userService.add(po);
        return R.ok();
    }

    @Verify(key = "bnt.sysUser.update")
    @PostMapping("/update")
    public R update(@RequestBody @Validated UserAddPo po) {
        if (po.getId() == null) {
            return R.fail().message("参数错误");
        }
        if (userService.count(new QueryWrapper<User>().lambda()
                .ne(User::getId, po.getId())
                .eq(User::getName, po.getUsername())) > 0) {
            return R.fail().message("角色名出现重复");
        }
        User User = new User();
        BeanUtils.copyProperties(po, User);
        userService.updateById(User);
        return R.ok();
    }

    @Verify(key = "bnt.sysUser.remove")
    @DeleteMapping("/removeById/{id}")
    public R removeById(@PathVariable Long id) {
        return userService.removeById(id) ?
                R.ok() : R.fail().message(id + "出现了错误");
    }

    @Verify(key = "bnt.sysUser.removeIds")
    @DeleteMapping("/removeByIds")
    public R removeByIds(@RequestBody List<Long> ids) {
        if (ids == null && ids.size() < 1) {
            return R.fail().message("参数错误");
        }
        userService.removeByIds(ids);
        return R.ok();
    }

    @Verify(key = "bnt.sysUser.update")
    @GetMapping("/changeStatus/{id}/{status}")
    public R changeStatus(@PathVariable Long id, @PathVariable byte status) {
        if (id < 1) {
            return R.fail().message("参数错误");
        }
        if (status < 0 || 1 < status) {
            return R.fail().message("参数错误");
        }
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        userService.updateById(user);
        return R.ok();
    }

    // 获取用户的角色
    @Verify(key = "bnt.sysUser.update")
    @GetMapping("/getByUserId/{userId}")
    public R getByUserId(@PathVariable Long userId) {
        if (userId < 1) {
            return R.fail().message("参数错误");
        }
        return R.ok(roleService.getByUserId(userId));
    }

    // 为用户分配角色
    @Verify(key = "bnt.sysUser.assignRole")
    @PostMapping("/assignRole")
    public R assignRole(@RequestBody @Validated AssignRolePo po) {
        if (userService.count(new LambdaQueryWrapper<User>().eq(User::getId, po.getUserId())) < 1) {
            return R.fail().message("用户不存在，不能添加角色");
        }
        roleService.assignRole(po);
        return R.ok();
    }

}
